package com.linglan.modules.department.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linglan.modules.department.dao.BizDepartmentDao;
import com.linglan.modules.department.dto.result.DepartmentResult;
import com.linglan.modules.department.entity.BizDepartment;
import com.linglan.modules.department.service.BizDepartmentService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 业务-科室表(BizDepartment)表服务实现类
 *
 * @author makejava
 * @since 2025-05-04 22:20:03
 */
@Service("bizDepartmentService")
public class BizDepartmentServiceImpl extends ServiceImpl<BizDepartmentDao, BizDepartment> implements BizDepartmentService {

    /**
     * 根据id查询科室并进行分组
     *
     * @param ids
     * @return
     */
    @Override
    public Map<Long, DepartmentResult> findGroupById(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return new HashMap<>();
        }
        LambdaQueryWrapper<BizDepartment> queryWrapper = Wrappers.lambdaQuery();
        if (CollectionUtils.size(ids) == 1) {
            queryWrapper.eq(BizDepartment::getId, ids.get(0));
        } else {
            queryWrapper.in(BizDepartment::getId, ids);
        }
        queryWrapper.eq(BizDepartment::getIsDeleted, 0);
        List<BizDepartment> list = this.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            Map<Long, DepartmentResult> map = new HashMap<>();
            for (BizDepartment bizDepartment : list) {
                DepartmentResult departmentResult = new DepartmentResult();
                departmentResult.setId(bizDepartment.getId());
                departmentResult.setHospitalId(bizDepartment.getHospitalId());
                departmentResult.setName(bizDepartment.getName());
                map.put(bizDepartment.getId(), departmentResult);
            }
            return map;
        }
        return new HashMap<>();
    }

    /**
     * 查询所有科室
     *
     * @return
     */
    @Override
    public List<DepartmentResult> findAllDepartments(Long hospitalId) {
        LambdaQueryWrapper<BizDepartment> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(BizDepartment::getHospitalId, hospitalId);
        queryWrapper.eq(BizDepartment::getIsDeleted, 0);
        queryWrapper.orderByDesc(BizDepartment::getWeight);
        List<BizDepartment> list = this.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            List<DepartmentResult> results = new ArrayList<>();
            for (BizDepartment bizDepartment : list) {
                DepartmentResult departmentResult = new DepartmentResult();
                departmentResult.setId(bizDepartment.getId());
                departmentResult.setHospitalId(bizDepartment.getHospitalId());
                departmentResult.setName(bizDepartment.getName());
                departmentResult.setAvatar(bizDepartment.getAvatar());
                departmentResult.setShortAvatar(bizDepartment.getShortAvatar());
                results.add(departmentResult);
            }
            return results;
        }
        return Collections.emptyList();
    }

    /**
     * 根据id查询科室信息
     *
     * @param id
     * @return
     */
    @Override
    public DepartmentResult findById(Long id) {
        LambdaQueryWrapper<BizDepartment> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(BizDepartment::getId, id);
        queryWrapper.eq(BizDepartment::getIsDeleted, 0);
        List<BizDepartment> list = this.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            BizDepartment bizDepartment = list.get(0);
            DepartmentResult departmentResult = new DepartmentResult();
            departmentResult.setId(bizDepartment.getId());
            departmentResult.setHospitalId(bizDepartment.getHospitalId());
            departmentResult.setName(bizDepartment.getName());
            departmentResult.setShortAvatar(bizDepartment.getShortAvatar());
            departmentResult.setAvatar(bizDepartment.getAvatar());
            departmentResult.setIntro(bizDepartment.getIntro());
            return departmentResult;
        }
        return null;
    }
}

